# -*- coding: utf-8 -*-
"""
Created on Tue Jul 25 16:49:11 2017
决策树 预测NBA比赛
@author: ZFH
"""

import pandas as pd
from collections import defaultdict
from sklearn.ensemble import RandomForestClassifier
import numpy as np
from sklearn.cross_validation import cross_val_score
import matplotlib.pyplot as plt

file_name = r'C:\Users\ZFH\Desktop\sportsref_download.csv'
# 载入文件
dataset = pd.read_csv(file_name, parse_dates=['Date'],skiprows=[1,])
# 给第一行重命名
dataset.columns = ["Date", "Start", "Visitor Team", "VisitorPts",
                   "Home Team", "HomePts", "Score Type", "OT?", "Notes"]
# 添加一列Homewin
dataset['Homewin'] = dataset['HomePts'] > dataset['VisitorPts']

y_true = dataset['Homewin'].values
# 定义一个默认字典
won_last = defaultdict(int)
# 添加两列
#==============================================================================
# dataset['Homelastwin'] = False
# dataset['Visitorlastwin'] = False
#==============================================================================

#==============================================================================
# for index,row in dataset.iterrows():
#     home_team = row['Home Team']
#     visitor_team = row['Visitor Team']
#     row['Homelastwin'] = won_last[home_team]
#     row['Visitorlastwin'] = won_last[visitor_team]
#     dataset.loc[index] = row    
#     won_last[home_team] = row["Homewin"]
#     won_last[visitor_team] = not row["Homewin"]
#==============================================================================
team = defaultdict(int)
for index, row in dataset.iterrows():
    home_team = row['Home Team']
    visitor_team = row['Visitor Team'] 
    if row['Homewin']:
        team[home_team] += 1
res = np.array(sorted(team.items(),key= lambda d:d[0]))
x = res[:,0]
y = res[:,1]
y = np.array(y,dtype=int)
plt.xticks(range(len(x)),x)
   
plt.bar(left = range(len(y)),height = y ,width = 0.35,align="center") 
plt.show










